home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / networking / amitcp / httpd.lha / httpd / http_log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-18  |  5.1 KB  |  165 lines

  1. /*
  2.  * http_log.c: Dealing with the logs and errors
  3.  * 
  4.  * Rob McCool
  5.  * 
  6.  */
  7.  
  8.  
  9. #include "httpd.h"
  10.  
  11. static FILE *error_log;
  12. static FILE *xfer_log;
  13.  
  14. void open_logs() {
  15.     if(!(xfer_log = fopen(xfer_fname,"a"))) {
  16.         fprintf(stderr,"httpd: could not open transfer log file %s.\n",
  17.                 xfer_fname);
  18.         perror("fopen");
  19.         exit(1);
  20.     }
  21.     if(!(error_log = fopen(error_fname,"a"))) {
  22.         fprintf(stderr,"httpd: could not open error log file %s.\n",
  23.                 error_fname);
  24.         perror("fopen");
  25.         exit(1);
  26.     }
  27. }
  28.  
  29. void close_logs() {
  30.     fclose(xfer_log);
  31.     fclose(error_log);
  32. }
  33.  
  34. void log_pid() {
  35. #ifndef NO_SECURITY
  36.     FILE *pid_file;
  37.  
  38.     if(!(pid_file = fopen(pid_fname,"w"))) {
  39.         fprintf(stderr,"httpd: could not log pid to file %s\n",pid_fname);
  40.         exit(1);
  41.     }
  42.     fprintf(pid_file,"%d\n",getpid());
  43.     fclose(pid_file);
  44. #endif
  45. }
  46.  
  47. void log_transaction(char *cmd_line) {
  48.     fprintf(xfer_log, "%s [%s] %s\n", remote_name, get_time(), cmd_line);
  49.     fclose(xfer_log); /* we should be done with it... */
  50. }
  51.  
  52. void log_error(char *err) {
  53.     fprintf(error_log, "[%s] %s\n",get_time(),err);
  54.     fclose(error_log);
  55. }
  56.  
  57. void log_reason(char *reason, char *file) {
  58.     char t[MAX_STRING_LEN];
  59.  
  60.     sprintf(t,"httpd: access to %s failed for %s, reason: %s",
  61.             file,remote_name,reason);
  62.     log_error(t);
  63. }
  64.  
  65. void error_head(char *err, FILE *fd) {
  66.     if(!assbackwards) {
  67.         fprintf(fd,"%s %s%c",SERVER_PROTOCOL,err,LF);
  68.         dump_default_header(fd);
  69.         fprintf(fd,"Content-type: text/html%c",LF);
  70.         fprintf(fd,"%c",LF);
  71.     }
  72.     if(!header_only) {
  73.         fprintf(fd,"<HEAD><TITLE>%s</TITLE></HEAD>%c",err,LF);
  74.         fprintf(fd,"<H1>%s</H1><BODY>%c",err,LF);
  75.     }
  76. }
  77.  
  78. void die(int type, char *err_string, FILE *fd) {
  79.     char t[MAX_STRING_LEN];
  80.  
  81.     switch(type) {
  82.       case REDIRECT:
  83.         if(!assbackwards) {
  84.             fprintf(fd,"%s %s%c",SERVER_PROTOCOL,"302 Found",LF);
  85.             fprintf(fd,"Location: %s%c",err_string,LF);
  86.             fprintf(fd,"Content-type: text/html%c",LF);
  87.             dump_default_header(fd);
  88.             fputc(LF,fd);
  89.         }
  90.         if(header_only) break;
  91.         fprintf(fd,"<HEAD><TITLE>Document moved</TITLE></HEAD>%c",LF);
  92.         fprintf(fd,"This document has moved <A HREF=\"%s\">here</A>.<P>%c",
  93.                 err_string,LF);
  94.         break;
  95.       case AUTH_REQUIRED:
  96.         if(!assbackwards) {
  97.             fprintf(fd,"%s %s%c",SERVER_PROTOCOL,"401 Unauthorized",LF);
  98.             dump_default_header(fd);
  99.             fprintf(fd,"WWW-Authenticate: %s%c%c",err_string,LF,LF);
  100.         }
  101.         if(header_only) break;
  102.         fprintf(fd,"<HEAD><TITLE>Authorization Required</TITLE></HEAD>%c",LF);
  103.         fprintf(fd,"Browser not authentication-capable or %c",LF);
  104.         fprintf(fd,"authentication failed.%c",LF);
  105.         break;
  106.       case BAD_REQUEST:
  107.         error_head("400 Bad Request",fd);
  108.         if(header_only) break;
  109.         fprintf(fd,"Your client sent a query that this server could not%c",LF);
  110.         fprintf(fd,"understand.<P>%c",LF);
  111.         fprintf(fd,"Reason: %s<P>%c",err_string,LF);
  112.         break;
  113.       case FORBIDDEN:
  114.         error_head("403 Forbidden",fd);
  115.         if(header_only) break;
  116.         fprintf(fd,"Your client does not have permission to get URL %s ",
  117.                 err_string);
  118.         fprintf(fd,"from this server.<P>%c",LF);
  119.         break;
  120.       case NOT_FOUND:
  121.         error_head("404 Not Found",fd);
  122.         if(header_only) break;
  123.         fprintf(fd,"The requested URL %s was not found on this server.<P>%c",
  124.                 err_string,LF);
  125.         break;
  126.       case SERVER_ERROR:
  127.         error_head("500 Server Error",fd);
  128.         log_error(err_string);
  129.         if(header_only) 
  130.             break;
  131.         fprintf(fd,"The server encountered an internal error or%c",LF);
  132.         fprintf(fd,"misconfiguration and was unable to complete your%c",LF);
  133.         fprintf(fd,"request.<P>%c",LF);
  134.         fprintf(fd,"Please contact the server administrator,%c",LF);
  135.         fprintf(fd," %s ",server_admin);
  136.         fprintf(fd,"and inform them of the time the error occured, and%c",LF);
  137.         fprintf(fd,"anything you might have done that may have caused%c",LF);
  138.         fprintf(fd,"the error.<P>%c",LF);
  139.         break;
  140.       case INCLUDE_ERROR:
  141.         error_head("500 Server Error",fd);
  142.         log_error(err_string);
  143.         if(header_only) break;
  144.         fprintf(fd,"The server was unable to serve you the document you%c",LF);
  145.         fprintf(fd,"requested, because %s.<P>%c",err_string,LF);
  146.         break;
  147.       case NOT_IMPLEMENTED:
  148.         error_head("501 Not Implemented",fd);
  149.         if(header_only) break;
  150.         fprintf(fd,"We are sorry to be unable to perform the method %s",
  151.                 err_string);
  152.         fprintf(fd," at this time.<P>%c",LF);
  153.         fprintf(fd,"If you would like to see this capability in future%c",LF);
  154.         fprintf(fd,"releases, send the method which failed, why you%c",LF);
  155.         fprintf(fd,"would like to have it, and the server version %s%c",
  156.                 SERVER_VERSION,LF);
  157.         fprintf(fd,"to <ADDRESS>%s</ADDRESS><P>%c",SERVER_SUPPORT,LF);
  158.         break;
  159.     }
  160.     if(!header_only)
  161.         fprintf(fd,"</BODY>%c",LF);
  162.     fclose(fd);
  163.     exit(1);
  164. }
  165.